﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NN
{
    public partial class InputParams : Form
    {
        MainWindow theMainWindow;

        public InputParams()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {            
            this.Hide();
            
        }
        public void setMainWindow(MainWindow win)
        {
            theMainWindow = win;
        }

        public void setInputRanges()
        {
            this.comboBox1.Items.Clear();
            this.comboBox1.Text = "";
            

            for(int a = 0; a < theMainWindow.theInputs.Count; a++)
            {
                this.comboBox1.Items.Add(theMainWindow.theInputs.ElementAt(a).VariableName);
            }
            
            this.textBox1.Text = theMainWindow.theInputs.ElementAt(0).LowerBounds.ToString();
            this.textBox2.Text = theMainWindow.theInputs.ElementAt(0).UpperBounds.ToString();

            this.comboBox1.SelectedIndex = 0;
            this.comboBox1.SelectedItem = this.comboBox1.Items[0];
            this.comboBox1.SelectedText = this.comboBox1.Items[0].ToString();

            this.comboBox1.Update();
        }

        public void setInputs(DataGridView theGrid)
        {
            this.dataGridView1.Columns.Clear();
            int colCount;           
            
            colCount = theMainWindow.theInputDataList.Count;            

            for (int a = 0; a < colCount; a++)
            {
                DataGridViewColumn tempColumn = new DataGridViewColumn();
                tempColumn.HeaderText = theMainWindow.theInputList.ElementAt(a);
                tempColumn.Name = theMainWindow.theInputList.ElementAt(a);
                this.comboBox1.Items.Add(tempColumn.Name);
                tempColumn.Width = 80;
                tempColumn.CellTemplate = theGrid.Columns[0].CellTemplate;
                this.dataGridView1.Columns.Add(tempColumn);

                if (this.dataGridView1.Rows.Count < theMainWindow.theInputs.ElementAt(a).PointCount())
                {
                    this.dataGridView1.Rows.Add(theMainWindow.theInputDataList.ElementAt(a).Count + 1);
                }
                for (int r = 0; r < theMainWindow.theInputDataList.ElementAt(a).Count; r++)
                {
                    this.dataGridView1.CurrentCell = dataGridView1[a, r];
                    this.dataGridView1.CurrentCell.ReadOnly = false;
                    this.dataGridView1.CurrentCell.Value = theMainWindow.theInputDataList.ElementAt(a).ElementAt(r);
                    this.dataGridView1.UpdateCellValue(a, r);
                    this.dataGridView1.CurrentCell.ReadOnly = true;
                }

            }

            colCount = this.dataGridView1.Rows.Count;
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int currentIndex;
            currentIndex = this.comboBox1.SelectedIndex;
            this.textBox1.Text = theMainWindow.theInputs.ElementAt(currentIndex).LowerBounds.ToString();
            this.textBox2.Text = theMainWindow.theInputs.ElementAt(currentIndex).UpperBounds.ToString();           
        }
        //reset bound values
        private void button2_Click(object sender, EventArgs e)
        {
            int currentIndex;
            currentIndex = this.comboBox1.SelectedIndex;
            if (currentIndex > -1)
            {
                theMainWindow.theInputs.ElementAt(currentIndex).calculateRange();

                this.textBox1.Text = theMainWindow.theInputs.ElementAt(currentIndex).LowerBounds.ToString();
                this.textBox2.Text = theMainWindow.theInputs.ElementAt(currentIndex).UpperBounds.ToString();
            }
        }

        //user redefines bounds
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.Items.Count > 0)
            {
                int currentIndex;
                currentIndex = this.comboBox1.SelectedIndex;
                try { theMainWindow.theInputs.ElementAt(currentIndex).LowerBounds = double.Parse(this.textBox1.Text); }
                catch
                {
                    MessageBox.Show("Lower Bounds invalid");
                }
                try { theMainWindow.theInputs.ElementAt(currentIndex).UpperBounds = double.Parse(this.textBox2.Text); }
                catch
                {
                    MessageBox.Show("Upper Bounds invalid");
                }
            }

        }


    }
}
